var app = getApp()
import ajaxhelper from '../../utils/ajaxhelper.js';
// import { signByMd5 } from "../../utils/util";
import checkLogin from '../../utils/checkLogin.js';
var goCommentPop = require("../common/goCommment/goCommment.js");//评论
var payBtn = require('../common/payBtn/payBtn.js');
Page(Object.assign({}, payBtn,{
  data: { 
    currentTime:0,//当前播放时间
    // currentTime1:80,
    isSubscribe: false,//是否已经订阅
    currentTab: 0, //预设当前项的值
    isMoreIntro: false,
    isFixed:false,//是否固定tab
    // showMaskComment:false,
    showMaskPlayList:false,
    curHdIndex2:0,//评价
    // textNum:0,//剩余评论字数
    canPlay: false,//是否可以播放
    isPlay:true,//是否在播放 以此决定是播放还是可以暂停
    curPlayIndex:0,//目前正在播放的音频序号
    cover:'',
    totalPrice:'',//购买价格
    canView: true,//默认可以观看
  },
  onLoad:function(options){
    var that = this;
    // console.log(options);
    var isFromShare = options.isFromShare || '';//是否是分享过来的
    if (options.isFromShare){
      var urlObj = options.urlObj || "{}";
      // 存储要播放的urlobj
      wx.setStorage({
        key: 'gloalObjUrl',
        data: urlObj,
      })
      console.log('1' + urlObj)
      urlObj = JSON.parse(urlObj);
      
    }else{
      var urlObj = wx.getStorageSync('gloalObjUrl');
      console.log('2' + urlObj)
      urlObj = urlObj ? JSON.parse(urlObj) : {};
    }
    that.setData({
      urlObj
    })
    //播放控制
    var playUrl = urlObj.url||'';
    // var index=options.index
    that.setData({
      playUrl: playUrl,
      curPlayIndex: urlObj.index,//正在播放的序号
      cover: urlObj.cover ||'http://image.chuangchuang.cn/chuangchuang/cc_other/20171221/20171221_1513820857788.jpg',
      canPlay: true,
      isFromShare,
    })
    // this.getAllDetail()
  },
  onShow(){
    // 检查有没有登录，第二个参数是回调函数
    checkLogin.login(this, function (res) {
      console.log(res)
      this.getAllDetail();//获取详情页面数据
    })
    console.log(app.backgroundAudioManager.src)
    // this.play(); 放在请求回的数据回调里面
    // this.setData({
    //   checkPlayList:false
    // })
  },
  getAllDetail() {
    var id = this.data.urlObj.courseId;
    var token = (wx.getStorageSync('Token_date') && wx.getStorageSync('Token_date').Token) || '';
    ajaxhelper.get(app.globalData.frontJSHost + 'knowledge/detailById/' + id + '?token=' + encodeURIComponent(token), {}, this, this.setAllDetail)
  },
  setAllDetail(res) {
    var that = this;
    var id = this.data.urlObj.id;
    if (res.result) {
      var indexList = res.data.indexList;
      if (indexList) {
        // 处理时间
        var resourcesList = []; //吧所有课程放到一个数组里面
        indexList.forEach(function (item, index, arr) {
          if (item.resources && item.resources.length > 0) {
            item.resources.forEach(function (itemRe, index, arr) {
              itemRe.lengthObj = app.timeFormat(itemRe.length);
              itemRe.createDate = new Date(+itemRe.createDate).format("yyyy-MM-dd hh:mm");
              resourcesList.push(itemRe);
              // 处理目前正在播放的
              if (itemRe.id==id){
                itemRe.isPlayIng = true
                that.setData({

                })
              }else{
                itemRe.isPlayIng = false
              }
            })
          }
        })
        res.data.resourcesList = resourcesList;
      }
      if (res.data.updateDate) {
        res.data.updateDate = new Date(+res.data.updateDate).format("yyyy年MM月dd日");
      }
      if (res.data.statistics) {
        res.data.statistics.avgPoint = res.data.statistics.avgPoint && res.data.statistics.avgPoint.toFixed(1);///保留一位小数
      }
      that.setData({
        lecturers: res.data.lecturers,//讲师
        course: res.data.course,//视频简介介绍
        indexList: res.data.indexList,//视频列表
        statistics: res.data.statistics,//评分评论
        subject: res.data.subject,//分类
        canView: res.data.canView,//是否可以播放
        resourcesList: res.data.resourcesList,//处理好的视频列表
        favoriteCount: res.data.favoriteCount,//订阅的个数
        isSubscribe: res.data.favoriteStatus,//是否订阅
        chapterCount: res.data.chapterCount,//总共集数
        updateDate: res.data.updateDate,//更新日期
        totalPrice: res.data.course.currentPrice,//购买价格
      })
      console.log(res)
      if (that.data.isFromShare) {
        //分享过来的 根据请求回来的canview来觉得取哪个url
        that.setData({
          playUrl: (that.data.canView || (!that.data.canView && that.data.resourcesList[0].snapshotFlag)) ? that.data.resourcesList[0].url : '',
        })
        var urlObj = wx.getStorageSync('gloalObjUrl');
        console.log('2' + urlObj)
        urlObj = urlObj ? JSON.parse(urlObj) : {};
        urlObj.url = (that.data.canView || (!that.data.canView && that.data.resourcesList[0].snapshotFlag)) ? that.data.resourcesList[0].url : '';
        console.log(3+urlObj.url)
        wx.setStorage({
          key: 'gloalObjUrl',
          data: JSON.stringify(urlObj),
        })
      }
    } else {
      wx.showToast({
        title: !!res.message.global ? res.message.global : "请求出错",
        icon: "loading"
      })
    }
    that.play();
  },
  // 播放
  play: function () {
    var that = this, duration = that.data.duration, currentTime = that.data.currentTime;
    var currUrl = app.backgroundAudioManager.src;
    var backgroundAudioManager=app.backgroundAudioManager;
    that.setData({
      isPlay: false
    })
    if (!that.data.canView && !that.data.playUrl) {
      wx.showToast({
        title: '请购买视听',
        icon: 'loading'
      })
      //解决slider停在中间的问题
      that.setData({
        duration: 0.001,
        currentTime: 0.001,
        isPlay: false
      })
      backgroundAudioManager.stop();
      backgroundAudioManager.src ='https:';
      backgroundAudioManager.title = '结果大学'
      backgroundAudioManager.epname = '课程';
      backgroundAudioManager.singer = '结果大学';
      backgroundAudioManager.seek(0);
      backgroundAudioManager.play();
      return false;
    }
    if (that.data.canView && !that.data.playUrl) {
      wx.showToast({
        title: '资源地址有误，请确认',
        icon: 'loading'
      })
      that.setData({
        duration: 0.001,
        currentTime: 0.001,
        isPlay: false
      })
      backgroundAudioManager.src = 'https:';
      backgroundAudioManager.title = '结果大学'
      backgroundAudioManager.epname = '课程';
      backgroundAudioManager.singer = '结果大学';
      backgroundAudioManager.seek(0);
      backgroundAudioManager.play();
      return false;
    }
    // if (!that.data.playUrl){
    //   wx.showToast({
    //     title: '资源地址有误，请确认',
    //     icon:'loading'
    //   })
    //   return false;
    // }
    if ((currUrl && currUrl != that.data.playUrl) || !currUrl){
      backgroundAudioManager.stop();
      backgroundAudioManager.src = that.data.playUrl;
      backgroundAudioManager.title = '结果大学'
      backgroundAudioManager.epname = '课程';
      backgroundAudioManager.singer = '结果大学';
      backgroundAudioManager.play();
      that.setData({
        isPlay: true
      })
      // backgroundAudioManager.title = '此时此刻'
      // backgroundAudioManager.epname = '此时此刻'
      // backgroundAudioManager.singer = '汪峰'
      // backgroundAudioManager.onTimeUpdate(function () {
      //   duration = Math.ceil(backgroundAudioManager.duration);
      //   currentTime = Math.ceil(backgroundAudioManager.currentTime)
      //   console.log(duration)
      //   that.setData({
      //     duration,
      //     currentTime
      //   })
      // })
      // currentTime = 0;
      // that.setData({
      //   currentTime
      // })
      
      // that.setTimesetInterval();
    } else if (currUrl && currUrl == that.data.playUrl){
      // 背景音乐正在播放  backgroundAudioManager.paused!=undefiend的目的是为了检测第一次播放音乐
      if (!backgroundAudioManager.paused) {
        duration = backgroundAudioManager.duration ? Math.ceil(backgroundAudioManager.duration) : 402;//视频长度
        currentTime = backgroundAudioManager.currentTime ? Math.ceil(backgroundAudioManager.currentTime) : 0;//背景音乐已经播放的位置;
        that.setData({
          duration,
          currentTime,
          isPlay: true
        })
        // that.setTimesetInterval();
      } else if (backgroundAudioManager.paused) {  //在暂停 checkPlayList判断是否是前一首下一首 如果为true意思就是重新获取url
        duration = backgroundAudioManager.duration ? Math.ceil(backgroundAudioManager.duration) : 402;//视频长度
        currentTime = backgroundAudioManager.currentTime ? Math.ceil(backgroundAudioManager.currentTime) : 0;//背景音乐已经播放的位置;
        that.setData({
          duration,
          currentTime,
          isPlay: false
        })
        // clearInterval(app.globalData.setIntervalId);
      }
    }else{

    }
    backgroundAudioManager.onTimeUpdate(function () {
      duration = Math.ceil(backgroundAudioManager.duration);
      currentTime = Math.ceil(backgroundAudioManager.currentTime)
      that.setData({
        duration,
        currentTime
      })
      that.timeupdateCount(backgroundAudioManager.currentTime, backgroundAudioManager.duration)
      // console.log('详情音频' + app.backgroundAudioManager.currentTime)
    })
    backgroundAudioManager.onEnded(function () {
      that.setData({
        duration:0,
        currentTime:0,
        isPlay:false
      })
      if (that.data.curPlayIndex >= that.data.resourcesList.length - 1) {
        wx.showToast({
          title: '最后一首了',
          icon: 'loading'
        })
        backgroundAudioManager.src="abc";
        backgroundAudioManager.stop();
        that.setData({
          curPlayIndex: that.data.resourcesList.length
        })
        return false
      }
      backgroundAudioManager.seek(0);
      // 获取全局音频信息 去缓存中取该资源是否付费
      var urlObj = wx.getStorageSync('gloalObjUrl');
      urlObj = urlObj ? JSON.parse(urlObj) : {};
      if (urlObj.canView) {
        that.setData({
          canView: true
        })
      }
      that.playNextAudio();
    })
    backgroundAudioManager.onError(function () {
      // clearInterval(app.globalData.setIntervalId);
      that.setData({
        // duration: 0,
        currentTime: 0,
        isPlay: false
      })
      backgroundAudioManager.seek(0);
    })
    
  },
  // 播放时间切换
  changePlayTime(e) {
    var that=this;
    var value = e.detail.value;
    if (app.backgroundAudioManager.paused){
      that.playAudio();
    }else{
      
    }
    app.backgroundAudioManager.seek(value);
    this.setData({
      currentTime: value
    })
  },
  // 开始播放时间记录
  setTimesetInterval(){
    var that=this;
    // clearInterval(app.globalData.setIntervalId);
    app.globalData.setIntervalId = setInterval(function () {
      that.data.currentTime++;
      that.setData({
        currentTime: that.data.currentTime
      })
    }, 1000)
  },
  // 播放上一首
  playPrevAudio(){
    var that=this;
    // that.setData({
    //   checkPlayList: true,
    // })
    if (that.data.curPlayIndex==0){
      wx.showToast({
        title: '已经是第一首',
        icon:'loading'
      })
    }else{
      // 获取下一首的地址
      // 判断是否为试听试看
      var curPlayIndex = that.data.curPlayIndex-1;
      var resourcesItem = that.data.resourcesList[curPlayIndex];
      var url = (that.data.canView || (!that.data.canView && resourcesItem.snapshotFlag)) ? resourcesItem.url : '';///////////视频地址是试听地址还是正式地址
      //var url = that.data.canView ? resourcesItem.url : resourcesItem.snapshotUrl;///////////视频地址是试听地址还是正式地址
      var id = resourcesItem.id;//课程中资源id
      var courseId =resourcesItem.courseId;//课程id
      var index = curPlayIndex;//课程序号
      var cover = resourcesItem.cover;//资源封面
      var resourceItem = resourcesItem;//资源全部信息
      var lecturers = that.data.lecturers;//导师
      var obj = {
        url,
        id,
        courseId,
        index,
        cover,
        resourceItem,
        lecturers
      }
   
      // 存储要播放的urlobj
      wx.setStorage({
        key: 'gloalObjUrl',
        data: JSON.stringify(obj),
      })
      that.setData({
        curPlayIndex: curPlayIndex,
        playUrl: url,
        cover: cover || 'http://image.chuangchuang.cn/chuangchuang/cc_other/20171221/20171221_1513820857788.jpg',
      })
      // clearInterval(app.globalData.setIntervalId)
      that.play()//调用播放
      //播放统计
      app.addHit(id, 'RESOURCE_AUDIO');
    }
    
  },
  // 播放下一首
  playNextAudio(){
    var that = this;
    // that.setData({
    //   checkPlayList:true,
    // })
    // // 获取下一首的地址
    // // var playUrl='';
    // // that.setData(){
    // //   playUrl: playUrl
    // // }
    // clearInterval(app.globalData.setIntervalId);
    // that.play()//调用播放
    ///////////////
    // var backgroundAudioManager = app.backgroundAudioManager;
    // // backgroundAudioManager.src='vedd';
    // // backgroundAudioManager.stop();
    // wx.stopBackgroundAudio()
    // that.setData({
    //   // duration: 0,
    //   currentTime: 0,
    //   isPlay: false
    // })
    //////////////
    if (that.data.curPlayIndex >= that.data.resourcesList.length-1) {
      wx.showToast({
        title: '最后一首了',
        icon: 'loading'
      })
      // that.setData({
      //   curPlayIndex: that.data.resourcesList.length
      // })
    } else {
      // 获取下一首的地址
      // 判断是否为试听试看
      var curPlayIndex =that.data.curPlayIndex+1;
      var resourcesItem = that.data.resourcesList[curPlayIndex];
      var url = (that.data.canView || (!that.data.canView && resourcesItem.snapshotFlag)) ? resourcesItem.url : '';///////////视频地址是试听地址还是正式地址
      //var url = that.data.canView ? resourcesItem.url : resourcesItem.snapshotUrl;///////////视频地址是试听地址还是正式地址
      var id = resourcesItem.id;//课程中资源id
      var courseId = resourcesItem.courseId;//课程id
      var index = curPlayIndex;//课程序号
      var cover = resourcesItem.cover;//资源封面
      var resourceItem = resourcesItem;//资源全部信息
      var lecturers = that.data.lecturers;//导师
      var obj = {
        url,
        id,
        courseId,
        index,
        cover,
        resourceItem,
        lecturers
      }
    
      // 存储要播放的urlobj
      wx.setStorage({
        key: 'gloalObjUrl',
        data: JSON.stringify(obj),
      })
      that.setData({
        curPlayIndex: curPlayIndex,
        playUrl: url,
        cover: cover || 'http://image.chuangchuang.cn/chuangchuang/cc_other/20171221/20171221_1513820857788.jpg',
      })
      // clearInterval(app.globalData.setIntervalId)
      that.play()//调用播放
      //播放统计
      app.addHit(id, 'RESOURCE_AUDIO');
    }
  },
  // 暂停播放
  pauseAudio(){
    var that=this;
    var curPlayIndex = that.data.curPlayIndex;
    if (!that.data.canView && !(that.data.resourcesList[curPlayIndex].snapshotFlag ? that.data.resourcesList[curPlayIndex].url:'')) {
      wx.showToast({
        title: '请购买视听',
        icon: 'loading'
      })
      return false;
    }
    if (that.data.canView && (that.data.resourcesList[curPlayIndex] &&!that.data.resourcesList[curPlayIndex].url)) {
      wx.showToast({
        title: '资源地址有误，请确认',
        icon: 'loading'
      })
      return false;
    }
    this.setData({
      isPlay: false
    })
    // clearInterval(app.globalData.setIntervalId);
    app.backgroundAudioManager.pause();
  },
  // 继续播放
  playAudio(){
    var that=this;
    var curPlayIndex = that.data.curPlayIndex;
    if (!that.data.canView && !(that.data.resourcesList[curPlayIndex].snapshotFlag ? that.data.resourcesList[curPlayIndex].url : '')) {
      wx.showToast({
        title: '请购买视听',
        icon: 'loading'
      })
      return false;
    }
    if (that.data.canView && (that.data.resourcesList[curPlayIndex] && !that.data.resourcesList[curPlayIndex].url)) {
      wx.showToast({
        title: '资源地址有误，请确认',
        icon: 'loading'
      })
      return false;
    }
    if (that.data.curPlayIndex > that.data.resourcesList.length - 1) {
      // 获取下一首的地址
      // 判断是否为试听试看
      var curPlayIndex = that.data.resourcesList.length-1;
      var resourcesItem = that.data.resourcesList[curPlayIndex];
      var url = (that.data.canView || (!that.data.canView && resourcesItem.snapshotFlag)) ? resourcesItem.url : '';///////////视频地址是试听地址还是正式地址
     // var url = that.data.canView ? resourcesItem.url : resourcesItem.snapshotUrl;///////////视频地址是试听地址还是正式地址
      var id = resourcesItem.id;//课程中资源id
      var courseId = resourcesItem.courseId;//课程id
      var index = curPlayIndex;//课程序号
      var cover = resourcesItem.cover;//资源封面
      var resourceItem = resourcesItem;//资源全部信息
      var lecturers = that.data.lecturers;//导师
      var obj = {
        url,
        id,
        courseId,
        index,
        cover,
        resourceItem,
        lecturers
      }
      // 存储要播放的urlobj
      wx.setStorage({
        key: 'gloalObjUrl',
        data: JSON.stringify(obj),
      })
      that.setData({
        curPlayIndex: curPlayIndex,
        playUrl: url,
        cover: cover || 'http://image.chuangchuang.cn/chuangchuang/cc_other/20171221/20171221_1513820857788.jpg',
      })
      // clearInterval(app.globalData.setIntervalId)
      that.play()//调用播放
      return false
    }
    this.setData({
      isPlay: true
    })
    // this.setTimesetInterval();
    app.backgroundAudioManager.play();
  },
  // 切换视频播放地址
  changePlayUrl(e) {
    console.log(e)
    var that=this;
    var url = e.currentTarget.dataset.url;
    var id = e.currentTarget.dataset.resourceId;//课程中资源id
    var courseId = e.currentTarget.dataset.courseId;//课程id
    var index = e.currentTarget.dataset.index;//课程序号
    var cover = e.currentTarget.dataset.cover;//资源封面
    var resourceItem = e.currentTarget.dataset.resourceItem;//资源全部信息
    var lecturers = that.data.lecturers;//导师
    var obj = {
      url,
      id,
      courseId,
      index,
      cover,
      resourceItem,
      lecturers
    }
    if (!that.data.canView && !url) {
      // wx.showToast({
      //   title: '请购买视听',
      //   icon: 'loading'
      // })
      //改为弹窗购买链接
      wx.showModal({
        title: '提示',
        content: '请购买该资源',
        confirmColor: '#FF4612',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定');
            that.goPay();//购买
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
      return false;
    }
    if (that.data.canView && !url) {
      wx.showToast({
        title: '资源地址有误，请确认',
        icon: 'loading'
      })
      return false;
    }
    // 存储要播放的urlobj
    wx.setStorage({
      key: 'gloalObjUrl',
      data: JSON.stringify(obj),
    })
    that.setData({
      curPlayIndex: index,
      playUrl: url,
      cover: cover || 'http://image.chuangchuang.cn/chuangchuang/cc_other/20171221/20171221_1513820857788.jpg',
      showMaskPlayList:false
    })
    
    // clearInterval(app.globalData.setIntervalId)
    that.play()//调用播放
    //播放统计
    app.addHit(id, 'RESOURCE_AUDIO');
  },
  // 是否展示全部内容
  isMoreIntro() {
    if (this.data.isMoreIntro) {
      this.setData({
        isMoreIntro: false
      })
    } else {
      this.setData({
        isMoreIntro: true
      })
    }
  },
  closeMask(e) {
    var id = e.currentTarget.dataset.id;
    this.setData({
      ['showMask' + id]: false
    });
  },
  showMask(e) {
    var id = e.currentTarget.dataset.id;
    this.setData({
      ['showMask'+id]: true
    });
  },
  // 弹出发表评论弹窗
  showCommentPop() {
    goCommentPop.set('goComment', {
      confirm: function (data) {
        console.log(data)
      }.bind(this)
    });
    goCommentPop.open('goComment')
  },
  // 查看更多评价
  lookAllComment() {
    console.log(4)
    wx.navigateTo({
      url: '../commentList/commentList',
    })
  },
  // 转换文稿
  goText(e) {
    console.log(e)
    var snapshotFlag = e.currentTarget.dataset.snapshotFlag;//是否可以试听
    if (this.data.canView || (!this.data.canView && snapshotFlag)) {
      var id = e.currentTarget.dataset.resourceId;//课程中资源id
      var courseId = e.currentTarget.dataset.courseId;//课程id
      var articleId = e.currentTarget.dataset.articleId;//文稿id
      console.log('id为'+articleId)
      if (articleId){
        wx.navigateTo({
          url: '../text/text?articleId=' + articleId,
        })
      }else{
        wx.showToast({
          title: '该资源没有文稿',
          icon: 'loading'
        })
      }
    } else {
      wx.showToast({
        title: '请先购买该资源',
        icon: 'loading'
      })
    }
  },
  // 订阅
  goSubscribe(e) {
    var id = e.currentTarget.dataset.courseId;
    var type = e.currentTarget.dataset.type;
    var token = (wx.getStorageSync('Token_date') && wx.getStorageSync('Token_date').Token) || '';
    if (this.data.isSubscribe) {
      // wx.showToast({
      //   title: '已经订阅过',
      //   icon: 'loading'
      // })
      // return false   
      var param = {
        "ids": [id],
        "sourceType": type
      }
      ajaxhelper.post(app.globalData.frontJSHost + 'favorite/delete?token=' + encodeURIComponent(token), param, this, function (res) {
        console.log(res)
        var that = this;
        if (res.result) {
          that.setData({
            isSubscribe: false,
            favoriteCount: that.data.favoriteCount-1,//本地订阅个数减
          })
          // wx.showModal({
          //   title: '已取消订阅！',
          //   content: '你可以点击订阅按钮再次订阅',
          //   showCancel: false,
          //   confirmText: '知道了',
          //   confirmColor: '#FF4612',
          //   success: function (res) {
          //   }
          // })
          wx.showToast({
            title: '已取消订阅',
            // icon: 'loading'
          })
        } else {
          wx.showToast({
            title: !!res.message.global ? res.message.global : "请求出错",
            icon: "loading"
          })
        }
      })
    } else {
      var param = {
        "sourceId": id,
        "sourceType": type
      }
      ajaxhelper.post(app.globalData.frontJSHost + 'favorite/add?token=' + encodeURIComponent(token), param, this, function (res) {
        console.log(res)
        var that = this;
        if (res.result) {
          that.setData({
            isSubscribe: true,
            favoriteCount: that.data.favoriteCount +1,//本地订阅个数加
          })
          // wx.showModal({
          //   title: '订阅成功！',
          //   content: '你可以在“订阅”中查看',
          //   showCancel: false,
          //   confirmText: '知道了',
          //   confirmColor: '#FF4612',
          //   success: function (res) {
          //     // if (res.confirm) {
          //     //   console.log('用户点击确定')
          //     // } else if (res.cancel) {
          //     //   console.log('用户点击取消')
          //     // }
          //   }
          // })
          wx.showToast({
            title: '订阅成功',
            // icon: 'loading'
          })
        } else {
          wx.showToast({
            title: !!res.message.global ? res.message.global : "请求出错",
            icon: "loading"
          })
        }
      })
    }


  },
  // 购买
  // goPay(e) {
  //   var that = this;
  //   // var courseItem = e.currentTarget.dataset.courseItem;
  //   /////////////
  //   // var coverMain = e.currentTarget.dataset.cover;
  //   // var name = e.currentTarget.dataset.name;
  //   // var currentPrice = e.currentTarget.dataset.price;
  //   // var updateDate = e.currentTarget.dataset.update;
  //   // var chapterCount = e.currentTarget.dataset.chapterCount || 0;//总共集数
  //   // var mediaType = e.currentTarget.dataset.mediaType;//类型
  //   // var id = e.currentTarget.dataset.sourceId;//资源id
  //   //////////
  //   var coverMain = that.data.course.coverMain;
  //   var name = that.data.course.name;
  //   var currentPrice = that.data.course.currentPrice;
  //   var updateDate = that.data.updateDate;
  //   var chapterCount = that.data.chapterCount || 0;//总共集数
  //   var mediaType = that.data.course.mediaType;//类型
  //   var id = that.data.course.id;//资源id
  //   var courseItem = {
  //     coverMain,
  //     name,
  //     currentPrice,
  //     updateDate,
  //     chapterCount,
  //     mediaType,
  //     id
  //   }
  //   courseItem = JSON.stringify(courseItem);
  //   wx.navigateTo({
  //     url: '../pay/pay?courseItem=' + courseItem,
  //   })
  // },
  // 分享
  onShareAppMessage: function (res) {
    var that = this;
    var resourcesItem = that.data.resourcesList[0];
    var url =''; //resourcesItem.url;
    var id = resourcesItem.id;//课程中资源id
    var courseId = resourcesItem.courseId;//课程id
    var index = 0;//课程序号
    var cover = resourcesItem.cover;//资源封面
    var resourceItem = resourcesItem;//资源全部信息
    var lecturers = that.data.lecturers;//导师
    var obj = {
      url,
      id,
      courseId,
      index,
      cover,
      resourceItem,
      lecturers
    }
    /////
    // var cover = this.data.course.coverMain || 'http://image.chuangchuang.cn/chuangchuang/cc_other/20171221/20171221_1513820857788.jpg';
    var title = this.data.course.name || '';
    return {
      title: title,
      path: '/pages/audioPlayDetail/audioPlayDetail?urlObj=' + JSON.stringify(obj) + '&isFromShare=true',//是否是分享过来的
      // imageUrl: cover ,//测试
      success: function (res) {
        // 转发成功
        console.log(res)
      },
      fail: function (res) {
        // 转发失败
        console.log(res)
      }
    }
  },
  // 播放进度变化
  timeupdateCount(currentTime, duration) {
    // console.log(currentTime);
    var currentTime = currentTime;
    var duration = duration;
    if ((currentTime - 10) > -0.1 && (currentTime - 10) < 0.25) {
      var curPlayIndex = this.data.curPlayIndex;
      var sourceId = this.data.resourcesList[curPlayIndex].id;
      app.playTimeCount(sourceId, 'RESOURCE_AUDIO', 'PLAY_10S');

    } else if ((currentTime - 3 * 60) > -0.1 && (currentTime - 3 * 60) < 0.25) {
      var curPlayIndex = this.data.curPlayIndex;
      var sourceId = this.data.resourcesList[curPlayIndex].id;
      app.playTimeCount(sourceId, 'RESOURCE_AUDIO', 'PLAY_3M');
    } else if ((currentTime - duration * 0.85) >= 0 && (currentTime - duration * 0.85) <= 0.5) {
      var curPlayIndex = this.data.curPlayIndex;
      var sourceId = this.data.resourcesList[curPlayIndex].id;
      app.playTimeCount(sourceId, 'RESOURCE_AUDIO', 'PLAY_END');
    }
  }
}))

